#include "mainwindow.h"
#include "rwxml.h"
#include "svgviewer.h"
#include "ECMAScript.h"

#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QPushButton>
#include <QLabel>
#include <QSplashScreen>
#include <QThread>
#include <QApplication>
#include <QMainWindow>
#include <QDesktopWidget>
#include <QScreen>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    JS::Run();

    //启动界面
    QSplashScreen *splash = new QSplashScreen();

    auto desktop = QApplication::desktop()->screen();
    int x = desktop->width()/2 - 300;
    int y = desktop->height()/2 -200;
    splash->setPixmap(QPixmap(":/bg.jpg"));//设置背景图片
    splash->setGeometry(x,y,600, 400);//设置窗口位置和大小

    auto font = splash->font();
    font.setPointSize(18);
    splash->setFont(font); //设置字体大小
    splash->show();

    splash->showMessage(QObject::tr("载入资源...")
                        ,Qt::AlignHCenter|Qt::AlignCenter
                        ,Qt::red);
    QThread::msleep(500); //载入资源
    splash->showMessage(QObject::tr("建立连接...")
                        ,Qt::AlignRight|Qt::AlignTop
                        ,Qt::blue);
    QThread::sleep(0.5); //载入资源
    splash->showMessage(QObject::tr("初始化界面...")
                        ,Qt::AlignRight|Qt::AlignBottom
                        ,Qt::green);

    QThread::sleep(0.5); //载入资源

    QWidget *win = new QWidget();
//    win->setAttribute(Qt::WA_QuitOnClose);

    win->setWindowTitle("Qt");
    win->setAttribute(Qt::WA_DeleteOnClose);//在窗口关闭时删除对象
    auto pbtn1 = new QPushButton();
    auto pbtn2 = new QPushButton();
    auto pbtn3 = new QPushButton();
    auto pbtn4 = new QPushButton();
    auto pbtn5 = new QPushButton();
    auto pbtn6 = new QPushButton();
    auto pbtn7 = new QPushButton();
    auto pbtn8 = new QPushButton();
    auto pbtnX = new QPushButton();

    pbtn1->setText("菜单、状态栏、存储、多文档");
    pbtn2->setText("切分窗口、滚动区域、停靠窗口");
    pbtn3->setText("事件过滤器");
    pbtn4->setText("绘图、拖放");
    pbtn5->setText("数据库、多线程");
    pbtn6->setText("XML");
    pbtn7->setText("Svg");
    pbtn8->setText("ECMAScript");
    pbtnX->setText("退出");

    //添加信号和槽
    QObject::connect(pbtn1,&QPushButton::clicked,[&]()
    {
        MainWindow* win1 = new MainWindow();
        win1->setAttribute(Qt::WA_DeleteOnClose);
//        win1->setAttribute(Qt::WA_QuitOnClose);
        win1->show();
        win->setDisabled(true);
        QObject::connect(win1,&QMainWindow::destroyed,[=]()
        {
            win->setDisabled(false);
        });
    });
    QObject::connect(pbtnX,&QPushButton::clicked,[&]()
    {
        win->close();
    });
    QObject::connect(pbtn6,&QPushButton::clicked,[&](){
        auto win1 = new RWXml();
        win1->setAttribute(Qt::WA_DeleteOnClose);
        win1->show();
        win->setDisabled(true);
        QObject::connect(win1,&QMainWindow::destroyed,[=]()
        {
            win->setDisabled(false);
        });
    });
    QObject::connect(pbtn7,&QPushButton::clicked,[&](){
        auto win1 = new SvgViewer();
        win1->setAttribute(Qt::WA_DeleteOnClose);
        win1->show();
        win->setDisabled(true);
        QObject::connect(win1,&QMainWindow::destroyed,[=]()
        {
            win->setDisabled(false);
        });
    });

    //添加布局管理器
    auto layout = new QGridLayout();
    layout->addWidget(pbtn1);
    layout->addWidget(pbtn2);
    layout->addWidget(pbtn3);
    layout->addWidget(pbtn4);
    layout->addWidget(pbtn5);
    layout->addWidget(pbtn6);
    layout->addWidget(pbtn7);
    layout->addWidget(pbtn8);

    layout->addWidget(pbtnX);
    win->setLayout(layout); //在窗口上安装布局管理器
    int w = win->sizeHint().width();
    int h = win->sizeHint().height();
    int x1 = (desktop->width() - w)/2;
    int y1 = (desktop->height() -h)/2;
    win->setGeometry(x1,y1,w,h);
    win->show();

    splash->finish(win); //结束载入界面
    delete splash;//释放载入界面资源

    app.exec();

    return 0;
}
